#import "ALHttpUtils.h"
//#import "ILHttpRequest.h"
#import "UIDevice+Hardware.h"
#include <ifaddrs.h>
#include <arpa/inet.h>
//#import "DESCryptor.h"
//#import "LSUrlConstants.h"
#import <CoreLocation/CoreLocation.h>
//#import "ILGPSManager.h"
//#import "ILGlobalVariable.h"
#import "ALUtils.h"
#import "ALMathHelper.h"
#import "JVNetworkReachability.h"

@implementation ALHttpUtils

+(NSString*)formatRandomUrl:(NSString*)url{
    NSRange rg = [url rangeOfString:@"?"];
    NSString* retUrl;
    if(rg.location==NSNotFound){
        retUrl = [NSString stringWithFormat:@"%@?__=%d",url,[ALMathHelper getRandomNumber:0 to:100000]];
    }else{
        retUrl = [NSString stringWithFormat:@"%@&__=%d",url,[ALMathHelper getRandomNumber:0 to:100000]];
    }
    return retUrl;
}



#pragma mark - HTTPS 后门 临时放在这里
//规则 RV3452016120108  2016年12月1号上午8点
- (void)longPressLogo:(UILongPressGestureRecognizer *)gesture{
    if(gesture.state == UIGestureRecognizerStateBegan){
        NSString *checkString = nil;
        NSString *dateStr = [ALUtils currentDateFormat:@"yyyyMMddHH"];
        NSString *nativeVersion = APP_VERSION;
        if (nativeVersion != nil && nativeVersion.length > 0) {
            NSString *trimVersion = [nativeVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
            checkString = [NSString stringWithFormat:@"R%@%@",trimVersion,dateStr];
        }else{
            checkString = dateStr;
        }
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        if ([pasteboard.string isEqualToString:checkString]){
            // 获取当前时间戳
            NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
            NSTimeInterval timestamp=[date timeIntervalSince1970];
            NSTimeInterval timestamp_i = ceil(timestamp);
            // 记录时间戳
            NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
            [ud setObject:[NSNumber numberWithDouble:timestamp_i] forKey:@"HTTPS_BACKDOOR_DATE"];
            [ud synchronize];
        }
    }
}

+(NSString*)formatHeaderStr{
    NSString * device = [[UIDevice currentDevice] hardwareString];
    NSString * gMobileAgentDevice = [device stringByReplacingOccurrencesOfString:@"," withString:@"_"];
    NSString * nativeVersion = APP_VERSION;
    NSString * h5ResVersion = nativeVersion;
    NSString* str = [NSString stringWithFormat:@"appVersion:%@,platform:ios,osVersion:%@,device:%@,resourceVersion:%@,channel:%@",nativeVersion,[UIDevice currentDevice].systemVersion,gMobileAgentDevice,h5ResVersion,@"app_store"];
    
    return str;
}
+ (NSString *)getIPAddress {
    
    NSString *address = @"";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            if(temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                    
                }
                
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    
    // 模拟器有时会得不到ip address，默认返回127.0.0.1
    if  ([address isEqualToString:@""]){
        address = @"127.0.0.1";
    }
    
    return address;
    
}

//获取网络类型
+ (NSString*)newtworkType {
    NSString *net ;
    JVNetworkReachability re = JVGetAccurateNetworkReachability();
    switch (re) {
        case JVNetworkReachabilityWifi:
            net=@"Wifi";
            break;
        case JVNetworkReachabilityMobile2G:
            net=@"2G";
            break;
        case JVNetworkReachabilityMobile3G:
            net=@"3G";
            break;
        case JVNetworkReachabilityMobile4G:
            net=@"4G";
            break;
        default:
            net=@"UNKNOWN";
            break;
    }
    return net;
}

@end
